package routers

import (
	middleware "aifuture.cloud/middleware/log"
	"aifuture.cloud/routers/api/course"
	"aifuture.cloud/routers/api/jiangzuo"
	"aifuture.cloud/routers/api/nav"
	"aifuture.cloud/routers/api/resource"
	"aifuture.cloud/routers/api/tp"
	"aifuture.cloud/routers/api/user"
	"github.com/gin-gonic/gin"
	"net/http"
)

// InitRouter initialize routing information
func InitRouter() *gin.Engine {
	r := gin.New()
	//r.Use(cors.Cors())
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	r.Use(middleware.Logger())
	r.Use(func(c *gin.Context) {
		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	})
	//深南教育导航栏注册路由
	nav.RouterRegister(r)
	//实训列表注册路由
	tp.RouterRegister(r)
	//资源中心注册路由
	resource.RouterRegister(r)
	//热门课程注册路由
	course.RouterRegister(r)
	//讲座注册路由
	jiangzuo.RouterRegister(r)
	//用户模块
	user.RouterRegister(r)
	return r
}
